const std = @import("std");
const jetzig = @import("jetzig");
const cate_service = @import("../../service/cate_service.zig");

pub const formats: jetzig.Route.Formats = .{
    .index = &.{.json},
};

/// 获取分类列表数据
pub fn index(req: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
    // 分类列表数据
    const cates = try cate_service.getCategories();

    var root = try data.object();
    try root.put("code", 200);
    try root.put("msg", "ok");

    var data_arr = try data.array();
    for (cates) |cate| {
        var cate_obj = try data.object();
        try cate_obj.put("id", data.integer(cate.id));
        try cate_obj.put("name", data.string(cate.name));

        try data_arr.append(cate_obj);
    }
    try root.put("data", data_arr);

    return req.render(.ok);
}
